﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;

namespace WSServer
{
    public class WSSocketCollection
    {
        static WSSocketCollection instance;
        public static WSSocketCollection collection
        {
            get
            {
                if(instance == null)
                {
                    instance = new WSSocketCollection();
                }
                return instance;
            }
        }

        private List<WSSocket> sockets { get; set; }
        private Timer Swapper { get; set; }
        public WSSocketCollection()
        {
            sockets = new List<WSSocket>();
        }

        public WSSocket this[int index]
        {
            get
            {
                return sockets[index];
            }
            set
            {
                sockets[index] = value;
            }
        }
        public void Enqueue(WSSocket socket)
        {
            sockets.Add(socket);
        }
        public WSSocket Find(Predicate<WSSocket> predictor)
        {
            return sockets.Find(predictor);
        }
        public void DeleteSocketWithIdentifier(string identifier)
        {
            WSSocket socket = Find(w=>
                {
                    return w.identifier == identifier;
                });
            if(socket != null)
            {
                socket.Dispose();
                sockets.Remove(socket);
            }
        }
    }
}
